/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clusterAnalysis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import units.CellType;
import units.MonkeyType;
import utils.Handle;

/**
 *
 * @author jamie
 */
public class ClusterStats {

    // calculate whether repeated genes from different sources appear more often in the same clusters than by random chance
    // calculate whether genes from clusters are unevenly distributed between the sources, both for individual clusters and as a whole
    // output data to make pie charts of cluster membership between sources
    // calculate whether certain HIV prots are responsible for different profiles

    private Handle handle = Handle.getConnectedHandle("monkeys");
    private TreeMap<Integer, List<Integer>> geneMap = new TreeMap<Integer, List<Integer>>();
    private TreeMap<Integer, List> monkeyTypeMap = new TreeMap<Integer, List>();
    private TreeMap<Integer, List> sourceMap = new TreeMap<Integer, List>();
    private TreeMap<Integer, List> cellTypeMap = new TreeMap<Integer, List>();
    private TreeMap<String, Set<Integer>> allGenesBySource = new TreeMap<String, Set<Integer>>();
    private boolean isgsOnly = true;

    public ClusterStats() {
        populateData();
    }

    private void populateData() {
        String sql = "select distinct t1.clusterId, t2.gId, t1.monkeyType, t1.cellType from clustermembership t1, probeIdLookup t2 where t1.probeId=t2.probeId and clusterId!=0 order by clusterId, t2.gId";
        ResultSet rs = handle.runSQLquery(sql);
        try {
            while (rs.next()) {
                int clusterId = rs.getInt("clusterId");
                int gId = rs.getInt("gId");
                MonkeyType mt = MonkeyType.valueOf(rs.getString("monkeyType"));
                CellType ct = CellType.valueOf(rs.getString("cellType"));
                String source = mt.name() + ct.name();
                if (!geneMap.containsKey(clusterId)) {
                    geneMap.put(clusterId, new ArrayList<Integer>());
                }
                if (!monkeyTypeMap.containsKey(clusterId)) {
                    monkeyTypeMap.put(clusterId, new ArrayList<Enum>());
                }
                if (!cellTypeMap.containsKey(clusterId)) {
                    cellTypeMap.put(clusterId, new ArrayList<Enum>());
                }
                if (!sourceMap.containsKey(clusterId)) {
                    sourceMap.put(clusterId, new ArrayList<String>());
                }
                if (!allGenesBySource.containsKey(source)) {
                    allGenesBySource.put(source, new TreeSet<Integer>());
                }
                geneMap.get(clusterId).add(gId);
                monkeyTypeMap.get(clusterId).add(mt);
                sourceMap.get(clusterId).add(source);
                cellTypeMap.get(clusterId).add(ct);
                allGenesBySource.get(source).add(gId);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ClusterStats.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void populateData_isgsOnly() {
        String sql = "select distinct t1.clusterId, t2.gId, t1.monkeyType, t1.cellType from clustermembership_isgsOnly t1, probeIdLookup t2 where t1.probeId=t2.probeId and clusterId!=0 order by clusterId, t2.gId";
        ResultSet rs = handle.runSQLquery(sql);
        try {
            while (rs.next()) {
                int clusterId = rs.getInt("clusterId");
                int gId = rs.getInt("gId");
                MonkeyType mt = MonkeyType.valueOf(rs.getString("monkeyType"));
                CellType ct = CellType.valueOf(rs.getString("cellType"));
                String source = mt.name() + ct.name();
                if (!geneMap.containsKey(clusterId)) {
                    geneMap.put(clusterId, new ArrayList<Integer>());
                }
                if (!monkeyTypeMap.containsKey(clusterId)) {
                    monkeyTypeMap.put(clusterId, new ArrayList<Enum>());
                }
                if (!cellTypeMap.containsKey(clusterId)) {
                    cellTypeMap.put(clusterId, new ArrayList<Enum>());
                }
                if (!sourceMap.containsKey(clusterId)) {
                    sourceMap.put(clusterId, new ArrayList<String>());
                }
                if (!allGenesBySource.containsKey(source)) {
                    allGenesBySource.put(source, new TreeSet<Integer>());
                }
                geneMap.get(clusterId).add(gId);
                monkeyTypeMap.get(clusterId).add(mt);
                sourceMap.get(clusterId).add(source);
                cellTypeMap.get(clusterId).add(ct);
                allGenesBySource.get(source).add(gId);
            }
        } catch (SQLException ex) {
            Logger.getLogger(ClusterStats.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void doCalculations() {
//        GeneRepeatStats stats = new GeneRepeatStats(geneMap);
//        stats.doRepeatGeneCalculations();
        ClusterSourceStats clusterSourceStats = new ClusterSourceStats(monkeyTypeMap, sourceMap, cellTypeMap, isgsOnly);
//        HIVProtInteractionStats hivProtInteractionStats = new HIVProtInteractionStats(geneMap);
//        ISGStats isgStats = new ISGStats(geneMap);

        
    }
}
